package com.ai.dtcore.service.impl;

import java.util.List;
import java.util.Map;

import com.ai.dtcore.service.IFlowDictionaryService;
import com.ai.dtcore.util.MyOwnRuntimeException;
import com.ai.dtcore.util.StringUtil;
import com.ai.frame.bean.InputObject;
import com.ai.frame.bean.OutputObject;
import com.ai.frame.logger.Logger;
import com.ai.frame.logger.LoggerFactory;

public class FlowDictionaryServiceImpl extends BaseServiceImpl implements IFlowDictionaryService {

	private static final Logger logger = LoggerFactory.getServiceLog(FlowDictionaryServiceImpl.class);
	
	//根据字段appliedRange（应用范围）查询符合条件的集合
	public void queryDictionaryListByRange(InputObject inputObject, OutputObject outputObject) throws MyOwnRuntimeException {
		Map<String, String> map = inputObject.getParams();
		//检查应用范围是否有值，当为空的时候不执行检索
		if(StringUtil.isEmpty(map.get("appliedRange"))){
			logger.info("queryDictionaryListByRange", "参数配置错误");
			throw new MyOwnRuntimeException("参数配置错误");
		}
		List<Map<String, String>> listDic = 
				this.getBaseDao().queryForList("queryDictionarByRange", map);
		outputObject.setBeans(listDic);
	}
	//查询字典项
	public void queryDictionaryList(InputObject inputObject, OutputObject outputObject) throws MyOwnRuntimeException {
		Map<String, String> map = inputObject.getParams();
		List<Map<String, String>> listDic = this.getBaseDao().queryForList("queryDictionaryList", map);
		outputObject.setBeans(listDic);
		Integer total = (Integer)getBaseDao().queryForObject("queryDictionaryListCount", map);
        outputObject.getBean().put("total", Integer.toString(total));
	}
	
	//新增字典项
	@Override
	public void insertDictionary(InputObject inputObject, OutputObject outputObject) throws MyOwnRuntimeException {
		Map<String, String> map = inputObject.getParams();
		if(StringUtil.isEmpty(map.get("dicName"))){
			logger.info("insertDictionary", "缺少参数dicName");
			throw new MyOwnRuntimeException("新增失败，缺少参数dicName");
		}
		if(StringUtil.isEmpty(map.get("dicValue"))){
			logger.info("insertDictionary", "缺少参数dicValue");
			throw new MyOwnRuntimeException("新增失败，缺少参数dicValue");
		}
		if(StringUtil.isEmpty(map.get("appliedRange"))){
			logger.info("insertDictionary", "缺少参数appliedRange");
			throw new MyOwnRuntimeException("新增失败，缺少参数appliedRange");
		}
		this.getBaseDao().insert("insertDictionary", map);
	}

	//修改字典项
	@Override
	public void updateDictionary(InputObject inputObject, OutputObject outputObject) throws MyOwnRuntimeException {
		Map<String, String> map = inputObject.getParams();
		if(StringUtil.isEmpty(map.get("dicName"))){
			logger.info("updateDictionary", "缺少参数dicName");
			throw new MyOwnRuntimeException("修改失败，缺少参数dicName");
		}
		if(StringUtil.isEmpty(map.get("dicValue"))){
			logger.info("updateDictionary", "缺少参数dicValue");
			throw new MyOwnRuntimeException("修改失败，缺少参数dicValue");
		}
		if(StringUtil.isEmpty(map.get("appliedRange"))){
			logger.info("updateDictionary", "缺少参数appliedRange");
			throw new MyOwnRuntimeException("修改失败，缺少参数appliedRange");
		}
		if(StringUtil.isEmpty(map.get("oldDicValue"))){
			logger.info("updateDictionary", "缺少参数oldDicValue");
			throw new MyOwnRuntimeException("修改失败，缺少参数oldDicValue");
		}
		if(StringUtil.isEmpty(map.get("oldAppliedRange"))){
			logger.info("updateDictionary", "缺少参数oldAppliedRange");
			throw new MyOwnRuntimeException("修改失败，缺少参数oldAppliedRange");
		}
		this.getBaseDao().update("updateDictionary", map);
	}

	//删除字典项
	@Override
	public void deleteDictionary(InputObject inputObject, OutputObject outputObject) throws MyOwnRuntimeException {
		Map<String, String> map = inputObject.getParams();
		logger.info("deleteDictionary", "删除");
		if(StringUtil.isEmpty(map.get("dicValue"))){
			logger.info("updateDictionary", "缺少参数dicValue");
			throw new MyOwnRuntimeException("修改失败，缺少参数dicValue");
		}
		if(StringUtil.isEmpty(map.get("appliedRange"))){
			logger.info("updateDictionary", "缺少参数appliedRange");
			throw new MyOwnRuntimeException("修改失败，缺少参数appliedRange");
		}
		this.getBaseDao().delete("deleteDictionary", map);
	}
}
